package com.aatattoo.light_food_service.controller;

import com.aatattoo.light_food_service.entity.*;
import com.aatattoo.light_food_service.enumeration.ConstantType;
import com.aatattoo.light_food_service.service.*;
import com.aatattoo.light_food_service.utils.TimeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author aatattoo
 * @Date 2021/12/13 11:52 上午
 * @msg:
 */
@Controller
@RequestMapping("/function")
@ResponseBody
public class FunctionController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private CommentService commentService;
    @Autowired
    ReplyService replyService;
    @Autowired
    private RemindService remindService;
    @Autowired
    private UserService userService;
    @Autowired
    private LikeService likeService;


    @PostMapping("dislike")
    public int dislikeDynamic(@RequestBody Like like,@RequestHeader("token")String token){
        int userId = tokenService.analysisUserId(token);
        like.setUserId(userId);
        return likeService.deleteLike(like);
    }
    @PostMapping("/like")
    public int likeDynamic(@RequestBody Like like,@RequestHeader("token")String token){
        int userId = tokenService.analysisUserId(token);
        like.setUserId(userId);
        System.out.println(like);
        return likeService.addLike(like);
    }

    @GetMapping("/comments/{dynamicId}/{pageNum}")
    public Map<String, Object> getComent(@PathVariable("dynamicId")int dynamicId, @PathVariable("pageNum")int pageNum, @RequestHeader("token")String token){
        PageHelper.startPage(pageNum,15);
        List<Object> comentList=new ArrayList<>();
        //检查是否有评论，有:查找评论，无直接返回空列表
        List<Comment> comments = commentService.findByDynamicId(dynamicId);
        PageInfo pageInfo = new PageInfo( comments );
        Map<String,Object> mapData=new LinkedHashMap<>();
        Map<String,Object> pageMap=new LinkedHashMap<>();
        pageMap.put("pageCount",pageInfo.getPages());
        pageMap.put("pageNumber",pageInfo.getPageNum());
        mapData.put("page",pageMap);
        int commentSize=comments.size();
        if (commentSize!=0){
            int userId = tokenService.analysisUserId(token);
            pageInfo.getList().forEach(list -> {
                Comment comment=(Comment)list;
                Map<String,Object> commetMap=new LinkedHashMap<>();
                commetMap.put("user",userService.getUserMap(comment.getUserId()));
                commetMap.put("id",comment.getId());
                commetMap.put("content",comment.getContent());
                //点赞字段
                Like comentLike=new Like();
                comentLike.setUserId(userId);
                comentLike.setType(ConstantType.LIKE_COMMENT.getType());
                comentLike.setParentId(comment.getId());
                System.out.println(comentLike.toString());
                System.out.println(likeService.checkLike(comentLike)!=0);
                commetMap.put("like",likeService.checkLike(comentLike)!=0);
                commetMap.put("countLike",likeService.countLike(comentLike));
                List<Object> commentRemindList=new ArrayList<>();
                if (comment.getHaveRemind()==ConstantType.COMMENT_REMIND.getType()){
                    Remind forReminds=new Remind();
                    forReminds.setType(ConstantType.REMIND_COMMENT.getType());
                    forReminds.setParentID(comment.getId());
                    List<Remind> reminds = remindService.getAllByParentId(forReminds);
                    reminds.forEach(remind -> {
                        User user = userService.fingById(remind.getPassiveId());
                        Map<String,Object> remindMap=new LinkedHashMap<>();
                        remindMap.put("id",remind.getPassiveId());
                        remindMap.put("userName",user.getName());
                        remindMap.put("order",remind.getOrder());
                        commentRemindList.add(remindMap);
                    });
                }
                commetMap.put("remind",commentRemindList);
                //子评论，@的用户信息
                List<Object> replyList=new ArrayList<>();
                System.out.println(comment.getHaveReply());
                if(comment.getHaveReply()==ConstantType.COMMENT_REPLY.getType()){
                    List<Reply> replies = replyService.findeByComentId(comment.getId());
                    replies.forEach(reply -> {
                        Map<String,Object> replyMap=new LinkedHashMap<>();
                        replyMap.put("user",userService.getUserMap(reply.getActiveId()));
                        replyMap.put("replyUser",userService.getUserMap(reply.getPassiveId()));
                        replyMap.put("id",reply.getId());
                        replyMap.put("content",reply.getContent());
                        //点赞字段
                        Like replyLike=new Like();
                        replyLike.setUserId(userId);
                        replyLike.setType(ConstantType.LIKE_REPLY.getType());
                        replyLike.setParentId(reply.getId());
                        replyMap.put("like",likeService.checkLike(replyLike)!=0);
                        replyMap.put("countLike",likeService.countLike(replyLike));
                        List<Object> remindList=new ArrayList<>();
                        if (reply.getHaveRemind()==ConstantType.COMMENT_REMIND.getType()){
                            Remind forReminds=new Remind();
                            forReminds.setType(ConstantType.REMIND_REPLY.getType());
                            forReminds.setParentID(reply.getId());
                            List<Remind> reminds = remindService.getAllByParentId(forReminds);
                            reminds.forEach(remind -> {
                                User user = userService.fingById(remind.getPassiveId());
                                Map<String,Object> remindMap=new LinkedHashMap<>();
                                remindMap.put("id",remind.getPassiveId());
                                remindMap.put("userName",user.getName());
                                remindMap.put("order",remind.getOrder());
                                remindList.add(remindMap);
                            });
                        }
                        replyMap.put("remind",remindList);
                        replyMap.put("createTime", TimeUtil.timestampFormat(reply.getCreateTime()));
                        replyList.add(replyMap);
                    });
                }
                commetMap.put("reply",replyList);
                commetMap.put("createTime",TimeUtil.timestampFormat(comment.getCreateTime()));
                comentList.add(commetMap);
            });
        }
        mapData.put("comment",comentList);
        return mapData;
    }

    @PostMapping("/dropComment")
    public int deleteComment(@RequestBody Comment comment){
        replyService.deleteByComent(comment.getId());
        return commentService.deleteById(comment.getId());
    }
    @PostMapping("/dropReply")
    public int deleteReply(@RequestBody Reply reply){
        return replyService.deleteById(reply.getId());
    }

    @PostMapping("/comment")
    public int addComment(@RequestBody CommentRelease commentRelease, @RequestHeader("token")String token){
        Comment comment =new Comment();
        int userId = tokenService.analysisUserId(token);
        int remindSize=commentRelease.getRemind().size();
        comment.setDynamicId(commentRelease.getDynamicId());
        comment.setUserId(userId);
        comment.setContent(commentRelease.getContent());
        comment.setHaveReply(ConstantType.COMMENT_UNREPLY.getType());
        comment.setHaveRemind(remindSize==0? ConstantType.COMMENT_UNREMIND.getType():ConstantType.COMMENT_REMIND.getType());
        int commentId = commentService.addComment(comment);
        if (remindSize!=0){
            commentRelease.getRemind().forEach(passiveId->{
                Remind remind=new Remind();
                remind.setActiveId(userId);
                remind.setPassiveId(passiveId);
                remind.setType(ConstantType.REMIND_COMMENT.getType());
                remind.setParentID(commentId);
                remind.setOrder(commentRelease.getRemind().indexOf(passiveId)+1);
                remindService.addRemind(remind);
            });
        }
        return commentId;
    }

    @PostMapping("/reply")
    public int addReply(@RequestBody ReplyRelease replyRelease,@RequestHeader("token")String token){
        Reply reply=new Reply();
        int userId = tokenService.analysisUserId(token);
        int remindSize=replyRelease.getRemind().size();
        reply.setCommentId(replyRelease.getCommentId());
        reply.setActiveId(userId);
        reply.setPassiveId(replyRelease.getUserId());
        reply.setContent(replyRelease.getContent());
        reply.setHaveRemind(remindSize==0? ConstantType.COMMENT_UNREMIND.getType():ConstantType.COMMENT_REMIND.getType());
        int replyId = replyService.addReply(reply);
        Comment comment=new Comment();
        comment.setId(reply.getCommentId());
        comment.setHaveReply(ConstantType.COMMENT_REPLY.getType());
        commentService.updataHaveReply(comment);
        if (remindSize!=0){
            replyRelease.getRemind().forEach(passiveId->{
                Remind remind=new Remind();
                remind.setActiveId(userId);
                remind.setPassiveId(passiveId);
                remind.setType(ConstantType.REMIND_REPLY.getType());
                remind.setParentID(replyId);
                remind.setOrder(replyRelease.getRemind().indexOf(passiveId)+1);
                remindService.addRemind(remind);
            });
        }
        return replyId;
    }



}
